package com.edu.controller;

import com.edu.component.PageBean;
import com.edu.component.QueryParam;
import com.edu.entity.Paper;
import com.edu.entity.PaperQuestion;
import com.edu.entity.Question;
//import com.edu.service.DicService;
import com.edu.service.PaperService;
import com.edu.service.QuestionService;
import com.edu.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Paper")
public class PaperController extends BaseController {
    @Autowired
    private PaperService paperService;
//    @Autowired
//    private DicService dicService;

    /*方法(1)-局部异常处理器
     * Fallback: 退路; 兜底方法, 应变措施
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public String exceptionFallBack(Exception e) {
        e.printStackTrace();
        return R.error(e).toJson();
    }

    /*方法(2)-保存试卷*/
    @PostMapping("/savePaper")
    @ResponseBody
    public R savePaper(Paper paper, HttpServletRequest req) {
        //req暂时先不用, 后面再考虑用途
        paper.setLevelId(10);
        paperService.savePaper(paper);
        return R.ok();
    }

    /*方法(3)-更新试卷*/
    @PostMapping("/updatePaper")
    @ResponseBody
    public R updatePaper(Paper paper) {
        paperService.updatePaper(paper);
        return R.ok();
    }

    /*方法(4)-试卷主页*/
    @GetMapping("/index")
    public String index(Model model) {
//        List<DicTypeData> levels = dicService.findLevels();
//        model.addAttribute("levels",levels);
        List<Map> levels = new ArrayList();
        HashMap m1 = new HashMap();
        m1.put("id", 10);
        m1.put("name", "初级");
        levels.add(m1);
        model.addAttribute("levels", levels);
        return "views/paper/paper_list";
    }

    /*方法(5)-试卷列表*/
    @GetMapping("/listPage")
    @ResponseBody
    public PageBean listPage(HttpServletRequest req) {
        //1.封装一个分页表单对象
        QueryParam QP = new QueryParam(req);
        //2.调用service查询列表数据
        return paperService.listPage(QP);
    }

    /*方法(6)-跳转到添加题目页*/
    @GetMapping("/appendQuestion")
    public String appendQuestion() {
        return "views/paper/add_question";
    }

    /*方法(7)-更新试卷*/

    /*方法(8)-删除试卷*/
    @PostMapping("/deletePaper/{id}")
    @ResponseBody
    public R deletePaper(
            @PathVariable("id") Integer id
    ) {
        paperService.deletePaper(id);
        return R.ok();
    }

    /*方法(9)-查询试卷 [组卷页中查试卷用]*/
    @PostMapping("/queryPaper")
    @ResponseBody
    public R queryPaper() {
        //1.查询全部试卷
        List<Paper> list = paperService.queryPaper();
        return R.data(list);
    }

    /*
     * 方法(10)-获取试卷的相关题目
     * 映射地址: /queryQuestionByPaperId/{paperId}
     * 携带路径参数: {paperId}
     * 应用场景: 组卷页中获取当前试卷的题目
     */
    @GetMapping("/queryQuestionByPaperId/{paperId}")
    @ResponseBody
    public R queryQuestionByPaperId(
            @PathVariable("paperId") Integer paperId
    ) {
        //1.调用service查询该试卷的题目
        List<PaperQuestion> list = paperService.queryQuestionByPaperId(paperId);
        return R.data(list);
    }

    /*方法(11)-保存手工组卷数*/
    @PostMapping("/diyPaperQuestion")
    @ResponseBody//实体表的paperQuestion 对象
    public R diyPaperQuestion(
            @RequestBody PaperQuestion paperQuestion) {
        paperService.diyPaperQuestion(paperQuestion);
        return R.ok();
    }

}


